package cn.qidisoft.edu.hzjt.service.xb.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.xb.PartyResponsibilityAttMapper;
import cn.qidisoft.edu.hzjt.dao.xb.PartyResponsibilityMapper;
import cn.qidisoft.edu.hzjt.model.xb.PartyResponsibilityAttEn;
import cn.qidisoft.edu.hzjt.model.xb.PartyResponsibilityEn;
import cn.qidisoft.edu.hzjt.service.xb.IPartyResponsibilityService;
import cn.qidisoft.edu.hzjt.utils.LogType;

@Service
public class PartyResponsibilityServiceImpl implements IPartyResponsibilityService {

	protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
			.getLogger(LogType.HZJT_LOGGER.val);

	@Autowired
	private PartyResponsibilityMapper mapper;

	@Autowired
	private PartyResponsibilityAttMapper mapperAtt;
	
	@Override
	public List<PartyResponsibilityEn> queryList(PartyResponsibilityEn model) {
		return mapper.queryList(model);
	}

	@Override
	public PaginationBean<PartyResponsibilityEn> queryPageList(PartyResponsibilityEn model,
			Page page) {
		try {
			int totalRecords = mapper.queryPageListcount(model);
			
			 PaginationBean<PartyResponsibilityEn> pageHolder = new PaginationBean<>(page, totalRecords);
	            List<PartyResponsibilityEn> resultList = mapper
	                    .queryPageList(model,
	                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
	                                    pageHolder.getPageSize()));
	            pageHolder.setPageList(resultList);
	            return pageHolder;
		} catch (Exception e) {
			LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(model, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
		}
	}

	@Override
	public PartyResponsibilityEn queryByPK(PartyResponsibilityEn model) {
		return mapper.queryByPK(model);
	}

	@Override
	public int insert(PartyResponsibilityEn model) {
		return mapper.insert(model);
	}

	@Override
	public int update(PartyResponsibilityEn model) {
		mapper.update(model);
		return 0;
	}

	@Override
	public int delete(PartyResponsibilityEn model) {
		mapper.delete(model);
		return 0;
	}

	@Override
	public List<Map<String, Object>> queryAllYearList() {
		return mapper.queryAllYearList();
	}

	@Override
	public PaginationBean<PartyResponsibilityEn> queryPageList2(PartyResponsibilityEn PartyResponsibilityEn,
			Page page) {
		try {
			int totalRecords = mapper.queryPageListcount2(PartyResponsibilityEn);
			
			 PaginationBean<PartyResponsibilityEn> pageHolder = new PaginationBean<>(page, totalRecords);
	            List<PartyResponsibilityEn> resultList = mapper
	                    .queryPageList2(PartyResponsibilityEn,
	                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
	                                    pageHolder.getPageSize()));


	            List<PartyResponsibilityEn> lst = new ArrayList<>();
				PartyResponsibilityAttEn att;
				for (PartyResponsibilityEn PartyResponsibilityEn2 : resultList) {
					PartyResponsibilityEn en = new PartyResponsibilityEn();
					ConvertUtils.register(new DateConverter(null), java.util.Date.class);

					BeanUtils.copyProperties(en, PartyResponsibilityEn2);
					att = new PartyResponsibilityAttEn();
					att.setPyear(PartyResponsibilityEn2.getZrNf());
					List<PartyResponsibilityAttEn> attList = mapperAtt.queryList2(att);
					en.setzRNFYear(PartyResponsibilityEn2.getZrNf()+"年");
					en.setAttList(attList);

					lst.add(en);
				}

				pageHolder.setPageList(lst);
				return pageHolder;
		} catch (Exception e) {
			LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(PartyResponsibilityEn, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
		}
	}

	@Override
	public List<PartyResponsibilityEn> exportList(PartyResponsibilityEn model, Page page) {
		try {
			PaginationBean<PartyResponsibilityEn> pageHolder = new PaginationBean<>(page, 999999);
            List<PartyResponsibilityEn> resultList = mapper
                    .queryPageList2(model,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));


            List<PartyResponsibilityEn> lst = new ArrayList<>();
			PartyResponsibilityAttEn att;
			for (PartyResponsibilityEn PartyResponsibilityEn2 : resultList) {
				PartyResponsibilityEn en = new PartyResponsibilityEn();
				ConvertUtils.register(new DateConverter(null), java.util.Date.class);

				BeanUtils.copyProperties(en, PartyResponsibilityEn2);
				att = new PartyResponsibilityAttEn();
				att.setPyear(PartyResponsibilityEn2.getZrNf());
				List<PartyResponsibilityAttEn> attList = mapperAtt.queryList2(att);
				en.setzRNFYear(PartyResponsibilityEn2.getZrNf()+"年");
				en.setAttList(attList);

				lst.add(en);
			}
			
			return lst;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
